<!--
 * @Description:
 * @Author: hui
 * @Date: 2022-02-17 18:23:45
 * @LastEditTime: 2022-03-31 10:43:25
-->
<template>
	<div class="information">
        <el-form ref="infoFormRef1" :model="infoForm1" status-icon :rules="loginRules" class="demo-ruleForm" label-width="280px">
            <el-form-item  prop="companyName" label="机构名称:" >
                <el-input v-model="infoForm1.companyName" placeholder="请输入企业或机构全称" class="span8"/>
            </el-form-item>
            <el-form-item  prop="creditNo" label="机构社会统一信用信息代码:">
                <el-input v-model="infoForm1.creditNo" placeholder="请输入" class="span8"/>
            </el-form-item>
            <!-- <el-form-item  prop="licenseNo" label="营业执照注册号:">
                <el-input v-model="infoForm1.licenseNo" placeholder="请输入" class="span8"/>
            </el-form-item> -->
            <el-form-item  prop="juridicalPerson" label="法定代表人姓名:">
                <el-input v-model="infoForm1.juridicalPerson" placeholder="请输入" class="span8"/>
            </el-form-item>
            <el-form-item  prop="juridicalPersonIdcard" label="法定代表人身份证号:">
                <el-input v-model="infoForm1.juridicalPersonIdcard" placeholder="请输入" class="span8"/>
            </el-form-item>
            <el-form-item class="span10">
                <el-button :loading="isSaveLoading" type="primary" @click="handleNext()"> 下一步 </el-button>
                <el-button @click="$router.back()"  type="default">返回</el-button>
            </el-form-item>
        </el-form>
	</div>
</template>
<script setup lang="ts">

import { isIdNumberRegular } from '@/utils/regularCommon';
import type { ElForm } from 'element-plus';
import { ComponentInternalInstance, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';

	const { appContext } = getCurrentInstance() as ComponentInternalInstance;
	const proxy = appContext.config.globalProperties;
	type FormInstance = InstanceType<typeof ElForm>;
	// 路由
	const router = useRouter();
	// vuex
	const store = useStore();
	// 登录
	const infoFormRef1 = ref();
    const isSaveLoading = ref(false);
	const infoForm1 = reactive({
		'companyName': ``,
		'creditNo': ``,
        // 'licenseNo':``,
        'id':``,
        'juridicalPerson':``,
        'juridicalPersonIdcard': ``,
	});
    let props = defineProps({
        type:{                        
            type: String,
        },
        formData:{
            type: Object,
            default: () => {}
        }
    })
    const emit = defineEmits(['selectActiveSign']);
 
	const loginRules = {
		'companyName': [
			{
				'required': true,
				'message': `请输入机构名称`,
				'trigger': `blur`
			},
		],
		'creditNo': [
			{
				'required': true,
				'message': `请输入机构社会统一信用信息代码`,
				'trigger': `blur`
			},
		],
        // 'licenseNo': [
		// 	{
		// 		'required': true,
		// 		'message': `请输入营业执照注册号`,
		// 		'trigger': `blur`
		// 	},
		// ],
        'juridicalPerson': [
			{
				'required': true,
				'message': `请输入法定代表人姓名`,
				'trigger': `blur`
			},
		],
        'juridicalPersonIdcard': [
			{
				'required': true,
				'message': `请输入法定代表人身份证号`,
				'trigger': `blur`,
			},
            { 'required': true, 'validator': isIdNumberRegular, 'trigger': `blur` }
		]
	};
    //跳转到下一步
    const handleNext = ()=>{
        infoFormRef1.value.validate(async (valid: boolean) => {
			if (valid) {
				isSaveLoading.value = true;
				const sentData = JSON.parse(JSON.stringify(infoForm1));
                if (sentData) {
                    // localStorage.setItem('companyInfo', JSON.stringify(sentData));
                    const activeSign = 'PublicInfo'
                    const type = 'next'
                    const companyName = infoForm1.companyName
                    emit('selectActiveSign', activeSign, type, companyName, '', sentData)
				} else {
					isSaveLoading.value = false;
				}
			}
		});
      
    };

    //获取企业实名信息
    // const getCompanyInfo= async(companyId: string)=>{
    //     try {
    //         const res = await getAppspaceCompany({id: companyId});
    //         Object.assign(infoForm1, {
    //             companyName: res.content.companyName,
    //             creditNo: res.content.creditNo,
    //             licenseNo: res.content.licenseNo,
    //             juridicalPerson: res.content.juridicalPerson,
    //             juridicalPersonIdcard: res.content.juridicalPersonIdcard,
    //         })
    //     } catch (error) {
    //         return Promise.reject(new Error(`cancel`));
    //     }
    // }

    watch(()=>props.formData, (newData, oldData) => {
        console.log(newData,"aa");
        Object.assign(infoForm1, newData)
	});

    const created = () => {
        console.log(props.formData,"props.formData888888====");
        Object.assign(infoForm1, props.formData)
       
        // if(props.type =='back'){
        //     Object.assign(infoForm1, props.formData)
        // }else{

        // }    
        
    };
    created();
   
	
</script>
<style lang="scss" scoped>
	.information {
		@extend %relative;
	}
     
</style>
